package com.example.automobile_positioning.activity;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import com.example.automobile_positioning.R;
import com.example.automobile_positioning.fragment.Locationfragment;
import com.example.automobile_positioning.fragment.Photofragment;
import com.example.automobile_positioning.util.LruCacheUtils;

public class MainActivity extends FragmentActivity implements OnClickListener,OnPageChangeListener{

	/**
	 * 图片缓存之内存缓存技术
	 */
	public static LruCacheUtils laCacheUtils =new LruCacheUtils();
	/**
	 * 内存缓存的键集合
	 */
	public static List<String> keys = new ArrayList<String>();
	public static List<Bitmap> bitmapsPhoto=new ArrayList<Bitmap>();
	private Button aaa;
	private TextView mLocation;
	private TextView mPhoto;
	private ViewPager mViewpage;
	private List<Fragment> fragments;
	private Locationfragment locationfragment;
	private Photofragment photofragment;
	private View mLine;
	private View mRiders;

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
		setContentView(R.layout.activity_main);
		initview();
	}


	//找到所以控件
	public void initview(){
		fragments=new ArrayList<Fragment>();
		locationfragment=new Locationfragment();
		photofragment=new Photofragment();
		fragments.add(locationfragment);
		fragments.add(photofragment);
		mLocation = (TextView) findViewById(R.id.tv_location);
		mPhoto = (TextView) findViewById(R.id.tv_photo);
		mViewpage = (ViewPager) findViewById(R.id.viewpage);
		mLine = findViewById(R.id.line_hot);
		mRiders = findViewById(R.id.line_riders);
		mLocation.setOnClickListener(this);
		mPhoto.setOnClickListener(this);
		mViewpage.setOnPageChangeListener(this);
		mViewpage.setAdapter(new Myfragment(getSupportFragmentManager()));
	}

	
  class Myfragment extends FragmentPagerAdapter{

	public Myfragment(FragmentManager fm) {
		super(fm);
		// TODO Auto-generated constructor stub
	}

	@Override
	public Fragment getItem(int arg0) {
		// TODO Auto-generated method stub
		return fragments.get(arg0);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return fragments.size();
	}
	  
  }

	
  /**
   * 改变顶部导航栏的颜色
   */
  private void textColor(int position) {
	  mLocation.setTextColor(getResources().getColor(R.color.text_gray));
	  mPhoto.setTextColor(getResources().getColor(R.color.text_gray));
      mLine.setVisibility(View.GONE);
      mRiders.setVisibility(View.GONE);
      switch (position) {
          case 0:
              mLocation.setTextColor(getResources().getColor(R.color.text_blue4));
              mLine.setVisibility(View.VISIBLE);
              break;
          case 1:
              mPhoto.setTextColor(getResources().getColor(R.color.text_blue4));
              mRiders.setVisibility(View.VISIBLE);
              break;
      }
  }
  
	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub
		
	}
	
	
	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		textColor(arg0);
		
	}
	
	//点击事件的方法
	@Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_hot:
                    locationfragment.newInstance();
                    mViewpage.setCurrentItem(0);
                break;
            case R.id.rl_riders:
                    photofragment.newInstance();
                    mViewpage.setCurrentItem(1);
                break;
        }
        
	}
}